package com.schoolcloud.social.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.social.entity.SocPostReplies;
import com.schoolcloud.social.query.PostRepliesQuery;
import com.schoolcloud.social.service.SocPostRepliesService;
import com.schoolcloud.social.mapper.SocPostRepliesMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author hongmizfb
 * @description 针对表【soc_post_replies(帖子回复表)】的数据库操作Service实现
 * @createDate 2025-02-15 19:48:06
 */
@Service
@RequiredArgsConstructor
public class SocPostRepliesServiceImpl extends ServiceImpl<SocPostRepliesMapper, SocPostReplies>
        implements SocPostRepliesService {
    @Override
    public Page<SocPostReplies> getSocRepliesPage(PostRepliesQuery query) {
        Integer parentId = query.getParentId();
        Integer postId = query.getPostId();
        return lambdaQuery()
                .eq(postId != null, SocPostReplies::getPostId, postId)
                .eq(parentId != null, SocPostReplies::getParentId, parentId)
                .page(query.toMpPage());
    }

    @Override
    public void addSocPostReply(SocPostReplies socPostReplies) {
        socPostReplies.setUserId(Integer.valueOf(UserContext.getUser()));
        socPostReplies.setCreateTime(new Date());
        save(socPostReplies);
    }

    @Override
    public Long countByPostId(Integer postId) {
        return lambdaQuery().eq(SocPostReplies::getPostId, postId).count();
    }
}




